using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using UnityEngine;
using UnityEngine.UI;

public class LocalManager : MonoBehaviour
{
	public enum LangType
	{
		key = 0,
		ru = 1,
		en = 2
	}

	public Dictionary<LangType, List<string>> Words;

	public LangType language = LangType.ru;

	public string path = "Assets/Resources/";

	public string file_name = "Localization";

	public Text[] translate_text;

	public static LocalManager Instance { get; private set; }

	private void Awake()
	{
		Instance = this;
		SetSystemLanguage();
	}

	private void Start()
	{
		Words = new Dictionary<LangType, List<string>>();
		Array values = Enum.GetValues(typeof(LangType));
		foreach (object item in values)
		{
			Words[(LangType)(int)item] = new List<string>();
		}
		LoadWords();
		Translation();
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.T))
		{
			NextLanguage();
		}
	}

	private void SetSystemLanguage()
	{
		if (PlayerPrefs.HasKey("lang"))
		{
			language = (LangType)PlayerPrefs.GetInt("lang");
		}
		else if (Application.systemLanguage == SystemLanguage.Russian)
		{
			language = LangType.ru;
		}
		else
		{
			language = LangType.en;
		}
	}

	private void SetLanguage(LangType lang)
	{
		language = lang;
		PlayerPrefs.SetInt("lang", (int)lang);
		Translation();
	}

	public void CreateFileTranslation()
	{
		string filename = path + file_name + ".xml";
		if (File.Exists(filename))
		{
			Debug.LogError("Файл Уже Создан");
			return;
		}
		XmlTextWriter xmlTextWriter = new XmlTextWriter(filename, Encoding.GetEncoding("utf-8"));
		xmlTextWriter.Formatting = Formatting.Indented;
		XmlDocument xmlDocument = new XmlDocument();
		XmlNode xmlNode = xmlDocument.CreateElement("Words");
		xmlDocument.AppendChild(xmlNode);
		Text[] array = translate_text;
		foreach (Text text in array)
		{
			XmlElement xmlElement = xmlDocument.CreateElement(text.name);
			XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("Ru");
			xmlAttribute.Value = string.Empty;
			XmlAttribute xmlAttribute2 = xmlDocument.CreateAttribute("En");
			xmlAttribute2.Value = string.Empty;
			xmlElement.SetAttributeNode(xmlAttribute);
			xmlElement.SetAttributeNode(xmlAttribute2);
			xmlNode.AppendChild(xmlElement);
		}
		xmlDocument.Save(xmlTextWriter);
		xmlTextWriter.Close();
	}

	public void LoadWords()
	{
		TextAsset textAsset = Resources.Load(file_name) as TextAsset;
		XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(textAsset.text));
		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load(xmlTextReader);
		XmlNode documentElement = xmlDocument.DocumentElement;
		foreach (XmlNode childNode in documentElement.ChildNodes)
		{
			Words[LangType.key].Add(childNode.Name);
			Words[LangType.ru].Add(GetString(childNode, "Ru"));
			Words[LangType.en].Add(GetString(childNode, "En"));
		}
		xmlTextReader.Close();
	}

	public string GetWord(string key)
	{
		for (int i = 0; i < Words[LangType.key].Count; i++)
		{
			if (Words[LangType.key][i] == key)
			{
				return Words[language][i];
			}
		}
		return string.Empty;
	}

	private void Translation()
	{
		Text[] array = translate_text;
		foreach (Text text in array)
		{
			text.text = GetWord(text.name);
		}
	}

	public void NextLanguage()
	{
		Array values = Enum.GetValues(typeof(LangType));
		int num = (int)language;
		num++;
		if (num >= values.Length)
		{
			num = 1;
		}
		SetLanguage((LangType)num);
	}

	private XmlAttribute attribute(string name, XmlDocument doc, string value)
	{
		XmlAttribute xmlAttribute = doc.CreateAttribute(name);
		xmlAttribute.Value = value;
		return xmlAttribute;
	}

	private string GetString(XmlNode node, string name)
	{
		try
		{
			return node.Attributes[name].Value;
		}
		catch
		{
			return "None";
		}
	}
}
